<?php


class ISubscribe_Payment_Gateway
{
	protected $params = array();
	protected $method = 'offsite'; // can be offsite or onsite
	protected $usesSSL = true;
	protected $config = array();
	protected $quickConfig = array();
	protected $subscriptionData = array();
	protected $priceData = array();
	protected $errors = array(); // used for reporting errors back to the controller
	protected $formErrors = array();

	public function __construct(){
		$this->params = array(
			'cc_name' => '',
			'cc_number' => '',
			'cc_expiry_year' => date('Y'),
			'cc_expire_month' => date('M'),
			'cc_ccv' => '',
			'return_url' => ''
		);
		$this->config = array(
			0 => array(
				'key' => 'username',
				'title' => 'Username / Account number',
				'value' => '',
				'type' => 'text'
			),
			1 => array(
				'key' => 'password',
				'title' => 'Password',
				'value' => '',
				'type' => 'text'
			),
			2 => array(
				'key' => 'url',
				'title' => 'URL to gateway processor',
				'value' => '',
				'type' => 'text'
			),
		);
	}

	public function getFormErrorsAsArray(){
		return $this->formErrors;
	}

	public function resetFormErrors(){
		$this->formErrors = array();
	}

	public function setSubscriptionData($subData){
		$this->subscriptionData = $subData;
	}

	public function getErrorsAsArray(){
		return $this->errors;
	}

	public function resetErrors(){
		$this->errors = array();
	}

	public function setPriceData($priceData){
		$this->priceData = $priceData;
	}

	// simply returns $this->config
	public function getConfigAsArray(){
		return $this->config;
	}

	// there is no checking of the data going on here. You must override this method and then call parent::setConfig!!
	public function setConfig($config){
		$this->config = $config;
		foreach($config as $k=>$v){
			$this->quickConfig[$v['key']] = $v['value'];
		};
		return true;
	}

	// get the value of a configuration setting, configuration settings can be entered by $this->setConfig
	public function getConfig($key){
		if(array_key_exists($key, $this->quickConfig)) return $this->quickConfig[$key];
		else return '';
	}

	public function getMethod(){
		return $this->method;
	}
	public function getButton($redirectingUrl){
		$btn = <<<BTN
<form action="{$redirectingUrl}" method="post">
	<input type="submit" value="Pay {$this->priceData['price']} Now" />
</form>
BTN;
		return $btn;
	}
	public function setParams($params){
		$this->params = array_merge($this->params, $params);
	}

	// called when the form is submitted and using offsite payment method
	public function sendOffsite(){
		// programmers must override this method to provide gateway specific logic
	}

	// called after returning from an offsite payment. returns true on success, false on failure
	public function checkOffsitePayment(){
		// programmers must override this method to provide gateway specific logic
	}

	// called after returning from a cancelled offsite payment. returns string of html to display
	public function cancelOffsitePayment(){
		// programmers must override this method to provide gateway specific logic
	}

	// called when a valid IPN request comes in. Nothing returned
	public function processIpn(){
		// programmers must override this method to provide gateway specific logic
	}

	// called after submitting the form, returns true on success, false on failure
	public function checkOnsitePayment(){
		// programmers must override this method to provide gateway specific logic
	}

}